<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>15.2. Importing Feeds</title>
<link rel="stylesheet" href="dbstyle.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.72.0">
<link rel="start" href="index.html" title="Programmer's Reference Guide">
<link rel="up" href="zend.feed.html" title="Chapter 15. Zend_Feed">
<link rel="prev" href="zend.feed.html" title="Chapter 15. Zend_Feed">
<link rel="next" href="zend.feed.findFeeds.html" title="15.3. Retrieving Feeds from Web Pages">
<link rel="chapter" href="introduction.html" title="Chapter 1. Introduction to Zend Framework">
<link rel="chapter" href="zend.acl.html" title="Chapter 2. Zend_Acl">
<link rel="chapter" href="zend.auth.html" title="Chapter 3. Zend_Auth">
<link rel="chapter" href="zend.cache.html" title="Chapter 4. Zend_Cache">
<link rel="chapter" href="zend.config.html" title="Chapter 5. Zend_Config">
<link rel="chapter" href="zend.console.getopt.html" title="Chapter 6. Zend_Console_Getopt">
<link rel="chapter" href="zend.controller.html" title="Chapter 7. Zend_Controller">
<link rel="chapter" href="zend.currency.html" title="Chapter 8. Zend_Currency">
<link rel="chapter" href="zend.date.html" title="Chapter 9. Zend_Date">
<link rel="chapter" href="zend.db.html" title="Chapter 10. Zend_Db">
<link rel="chapter" href="zend.debug.html" title="Chapter 11. Zend_Debug">
<link rel="chapter" href="zend.dojo.html" title="Chapter 12. Zend_Dojo">
<link rel="chapter" href="zend.dom.html" title="Chapter 13. Zend_Dom">
<link rel="chapter" href="zend.exception.html" title="Chapter 14. Zend_Exception">
<link rel="chapter" href="zend.feed.html" title="Chapter 15. Zend_Feed">
<link rel="chapter" href="zend.filter.html" title="Chapter 16. Zend_Filter">
<link rel="chapter" href="zend.form.html" title="Chapter 17. Zend_Form">
<link rel="chapter" href="zend.gdata.html" title="Chapter 18. Zend_Gdata">
<link rel="chapter" href="zend.http.html" title="Chapter 19. Zend_Http">
<link rel="chapter" href="zend.infocard.html" title="Chapter 20. Zend_InfoCard">
<link rel="chapter" href="zend.json.html" title="Chapter 21. Zend_Json">
<link rel="chapter" href="zend.layout.html" title="Chapter 22. Zend_Layout">
<link rel="chapter" href="zend.ldap.html" title="Chapter 23. Zend_Ldap">
<link rel="chapter" href="zend.loader.html" title="Chapter 24. Zend_Loader">
<link rel="chapter" href="zend.locale.html" title="Chapter 25. Zend_Locale">
<link rel="chapter" href="zend.log.html" title="Chapter 26. Zend_Log">
<link rel="chapter" href="zend.mail.html" title="Chapter 27. Zend_Mail">
<link rel="chapter" href="zend.measure.html" title="Chapter 28. Zend_Measure">
<link rel="chapter" href="zend.memory.html" title="Chapter 29. Zend_Memory">
<link rel="chapter" href="zend.mime.html" title="Chapter 30. Zend_Mime">
<link rel="chapter" href="zend.openid.html" title="Chapter 31. Zend_OpenId">
<link rel="chapter" href="zend.paginator.html" title="Chapter 32. Zend_Paginator">
<link rel="chapter" href="zend.pdf.html" title="Chapter 33. Zend_Pdf">
<link rel="chapter" href="zend.registry.html" title="Chapter 34. Zend_Registry">
<link rel="chapter" href="zend.rest.html" title="Chapter 35. Zend_Rest">
<link rel="chapter" href="zend.search.lucene.html" title="Chapter 36. Zend_Search_Lucene">
<link rel="chapter" href="zend.server.html" title="Chapter 37. Zend_Server">
<link rel="chapter" href="zend.service.html" title="Chapter 38. Zend_Service">
<link rel="chapter" href="zend.session.html" title="Chapter 39. Zend_Session">
<link rel="chapter" href="zend.soap.html" title="Chapter 40. Zend_Soap">
<link rel="chapter" href="zend.test.html" title="Chapter 41. Zend_Test">
<link rel="chapter" href="zend.text.html" title="Chapter 42. Zend_Text">
<link rel="chapter" href="zend.timesync.html" title="Chapter 43. Zend_TimeSync">
<link rel="chapter" href="zend.translate.html" title="Chapter 44. Zend_Translate">
<link rel="chapter" href="zend.uri.html" title="Chapter 45. Zend_Uri">
<link rel="chapter" href="zend.validate.html" title="Chapter 46. Zend_Validate">
<link rel="chapter" href="zend.version.html" title="Chapter 47. Zend_Version">
<link rel="chapter" href="zend.view.html" title="Chapter 48. Zend_View">
<link rel="chapter" href="zend.xmlrpc.html" title="Chapter 49. Zend_XmlRpc">
<link rel="appendix" href="requirements.html" title="Appendix A. Zend Framework Requirements">
<link rel="appendix" href="coding-standard.html" title="Appendix B. Zend Framework Coding Standard for PHP">
<link rel="appendix" href="copyrights.html" title="Appendix C. Copyright Information">
<link rel="index" href="the.index.html" title="Index">
<link rel="subsection" href="zend.feed.importing.html#zend.feed.importing.custom" title="15.2.1. Custom feeds">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<div class="navheader"><table width="100%" summary="Navigation header">
<tr><th colspan="3" align="center">15.2. Importing Feeds</th></tr>
<tr>
<td width="20%" align="left">
<a accesskey="p" href="zend.feed.html">Prev</a> </td>
<th width="60%" align="center">Chapter 15. Zend_Feed</th>
<td width="20%" align="right"> <a accesskey="n" href="zend.feed.findFeeds.html">Next</a>
</td>
</tr>
</table></div>
<div class="sect1" lang="en">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="zend.feed.importing"></a>15.2. Importing Feeds</h2></div></div></div>
<p>
        <code class="code">Zend_Feed</code> enables developers to retrieve feeds very easily. If you know the URI of a feed, simply
        use the <code class="code">Zend_Feed::import()</code> method:
    </p>
<pre class="programlisting">&lt;?php
$feed = Zend_Feed::import('http://feeds.example.com/feedName');
    </pre>
<p>
        You can also use <code class="code">Zend_Feed</code> to fetch the contents of a feed from a file or the contents of a PHP
        string variable:
    </p>
<pre class="programlisting">&lt;?php
// importing a feed from a text file
$feedFromFile = Zend_Feed::importFile('feed.xml');

// importing a feed from a PHP string variable
$feedFromPHP = Zend_Feed::importString($feedString);
    </pre>
<p>
        In each of the examples above, an object of a class that extends <code class="code">Zend_Feed_Abstract</code> is returned
        upon success, depending on the type of the feed. If an RSS feed were retrieved via one of the import methods
        above, then a <code class="code">Zend_Feed_Rss</code> object would be returned. On the other hand, if an Atom feed were
        imported, then a <code class="code">Zend_Feed_Atom</code> object is returned. The import methods will also throw a
        <code class="code">Zend_Feed_Exception</code> object upon failure, such as an unreadable or malformed feed.
    </p>
<div class="sect2" lang="en">
<div class="titlepage"><div><div><h3 class="title">
<a name="zend.feed.importing.custom"></a>15.2.1. Custom feeds</h3></div></div></div>
<p>
            <code class="code">Zend_Feed</code> enables developers to create custom feeds very easily. You just have to create an
            array and to import it with Zend_Feed. This array can be imported with <code class="code">Zend_Feed::importArray()</code>
            or with <code class="code">Zend_Feed::importBuilder()</code>. In this last case the array will be computed on the fly by
            a custom data source implementing <code class="code">Zend_Feed_Builder_Interface</code>.
        </p>
<div class="sect3" lang="en">
<div class="titlepage"><div><div><h4 class="title">
<a name="zend.feed.importing.custom.importarray"></a>15.2.1.1. Importing a custom array</h4></div></div></div>
<pre class="programlisting">&lt;?php
// importing a feed from an array
$atomFeedFromArray = Zend_Feed::importArray($array);

// the following line is equivalent to the above; by default a Zend_Feed_Atom instance is returned
$atomFeedFromArray = Zend_Feed::importArray($array, 'atom');

// importing a rss feed from an array
$rssFeedFromArray = Zend_Feed::importArray($array, 'rss');
            </pre>
<p>
                The format of the array must conform to this structure:
            </p>
<pre class="programlisting">&lt;?php
array(
      'title'       =&gt; 'title of the feed', //required
      'link'        =&gt; 'canonical url to the feed', //required
      'lastUpdate'  =&gt; 'timestamp of the update date', // optional
      'published'   =&gt; 'timestamp of the publication date', //optional
      'charset'     =&gt; 'charset of the textual data', // required
      'description' =&gt; 'short description of the feed', //optional
      'author'      =&gt; 'author/publisher of the feed', //optional
      'email'       =&gt; 'email of the author', //optional
      'webmaster'   =&gt; 'email address for person responsible for technical issues' // optional, ignored if atom is used
      'copyright'   =&gt; 'copyright notice', //optional
      'image'       =&gt; 'url to image', //optional
      'generator'   =&gt; 'generator', // optional
      'language'    =&gt; 'language the feed is written in', // optional
      'ttl'         =&gt; 'how long in minutes a feed can be cached before refreshing', // optional, ignored if atom is used
      'rating'      =&gt; 'The PICS rating for the channel.', // optional, ignored if atom is used
      'cloud'       =&gt; array(
                             'domain'            =&gt; 'domain of the cloud, e.g. rpc.sys.com' // required
                             'port'              =&gt; 'port to connect to' // optional, default to 80
                             'path'              =&gt; 'path of the cloud, e.g. /RPC2' //required
                             'registerProcedure' =&gt; 'procedure to call, e.g. myCloud.rssPleaseNotify' // required
                             'protocol'          =&gt; 'protocol to use, e.g. soap or xml-rpc' // required
                             ), // a cloud to be notified of updates // optional, ignored if atom is used
      'textInput'   =&gt; array(
                             'title'       =&gt; 'the label of the Submit button in the text input area' // required,
                             'description' =&gt; 'explains the text input area' // required
                             'name'        =&gt; 'the name of the text object in the text input area' // required
                             'link'        =&gt; 'the URL of the CGI script that processes text input requests' // required
                             ) // a text input box that can be displayed with the feed // optional, ignored if atom is used
      'skipHours'   =&gt; array(
                             'hour in 24 format', // e.g 13 (1pm)
                             // up to 24 rows whose value is a number between 0 and 23
                             ) // Hint telling aggregators which hours they can skip // optional, ignored if atom is used
      'skipDays '   =&gt; array(
                             'a day to skip', // e.g Monday
                             // up to 7 rows whose value is a Monday, Tuesday, Wednesday, Thursday, Friday, Saturday or Sunday
                             ) // Hint telling aggregators which days they can skip // optional, ignored if atom is used
      'itunes'      =&gt; array(
                             'author'       =&gt; 'Artist column' // optional, default to the main author value
                             'owner'        =&gt; array(
                                                     'name' =&gt; 'name of the owner' // optional, default to main author value
                                                     'email' =&gt; 'email of the owner' // optional, default to main email value
                                                     ) // Owner of the podcast // optional
                             'image'        =&gt; 'album/podcast art' // optional, default to the main image value
                             'subtitle'     =&gt; 'short description' // optional, default to the main description value
                             'summary'      =&gt; 'longer description' // optional, default to the main description value
                             'block'        =&gt; 'Prevent an episode from appearing (yes|no)' // optional
                             'category'     =&gt; array(
                                                     array('main' =&gt; 'main category', // required
                                                           'sub'  =&gt; 'sub category' // optional
                                                           ),
                                                     // up to 3 rows
                                                     ) // 'Category column and in iTunes Music Store Browse' // required
                             'explicit'     =&gt; 'parental advisory graphic (yes|no|clean)' // optional
                             'keywords'     =&gt; 'a comma separated list of 12 keywords maximum' // optional
                             'new-feed-url' =&gt; 'used to inform iTunes of new feed URL location' // optional
                             ) // Itunes extension data // optional, ignored if atom is used
      'entries'     =&gt; array(
                             array(
                                   'title'        =&gt; 'title of the feed entry', //required
                                   'link'         =&gt; 'url to a feed entry', //required
                                   'description'  =&gt; 'short version of a feed entry', // only text, no html, required
                                   'guid'         =&gt; 'id of the article, if not given link value will used', //optional
                                   'content'      =&gt; 'long version', // can contain html, optional
                                   'lastUpdate'   =&gt; 'timestamp of the publication date', // optional
                                   'comments'     =&gt; 'comments page of the feed entry', // optional
                                   'commentRss'   =&gt; 'the feed url of the associated comments', // optional
                                   'source'       =&gt; array(
                                                           'title' =&gt; 'title of the original source' // required,
                                                           'url' =&gt; 'url of the original source' // required
                                                           ) // original source of the feed entry // optional
                                   'category'     =&gt; array(
                                                           array(
                                                                 'term' =&gt; 'first category label' // required,
                                                                 'scheme' =&gt; 'url that identifies a categorization scheme' // optional
                                                                 ),
                                                           array(
                                                                 //data for the second category and so on
                                                                 )
                                                           ) // list of the attached categories // optional
                                   'enclosure'    =&gt; array(
                                                           array(
                                                                 'url' =&gt; 'url of the linked enclosure' // required
                                                                 'type' =&gt; 'mime type of the enclosure' // optional
                                                                 'length' =&gt; 'length of the linked content in octets' // optional
                                                                 ),
                                                           array(
                                                                 //data for the second enclosure and so on
                                                                 )
                                                           ) // list of the enclosures of the feed entry // optional
                                   ),
                             array(
                                   //data for the second entry and so on
                                   )
                             )
       );
            </pre>
<p>
                References:
                </p>
<div class="itemizedlist"><ul type="disc">
<li><p>
                            RSS 2.0 specification: <a href="http://blogs.law.harvard.edu/tech/rss" target="_top">RSS 2.0</a>
                        </p></li>
<li><p>
                            Atom specification: <a href="http://tools.ietf.org/html/rfc4287" target="_top">RFC 4287</a>
                        </p></li>
<li><p>
                            WFW specification: <a href="http://wellformedweb.org/news/wfw_namespace_elements" target="_top">Well
                            Formed Web</a>
                        </p></li>
<li><p>
                            iTunes specification:
                            <a href="http://www.apple.com/itunes/store/podcaststechspecs.html" target="_top">iTunes Technical
                            Specifications</a>
                        </p></li>
</ul></div>
<p>
            </p>
</div>
<div class="sect3" lang="en">
<div class="titlepage"><div><div><h4 class="title">
<a name="zend.feed.importing.custom.importbuilder"></a>15.2.1.2. Importing a custom data source</h4></div></div></div>
<p>
                You can create a Zeed_Feed instance from any data source implementing
                <code class="code">Zend_Feed_Builder_Interface</code>. You just have to implement the <code class="code">getHeader()</code> and
                <code class="code">getEntries()</code> methods to be able to use your object with
                <code class="code">Zend_Feed::importBuilder()</code>. As a simple reference implementation, you can use
                <code class="code">Zend_Feed_Builder</code>, which takes an array in its constructor, performs some minor validation,
                and then can be used in the <code class="code">importBuilder()</code> method. The <code class="code">getHeader()</code> method
                must return an instance of <code class="code">Zend_Feed_Builder_Header</code>, and <code class="code">getEntries()</code> must
                return an array of <code class="code">Zend_Feed_Builder_Entry</code> instances.
            </p>
<div class="note"><table border="0" summary="Note">
<tr>
<td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="images/note.png"></td>
<th align="left">Note</th>
</tr>
<tr><td align="left" valign="top"><p>
                    <code class="code">Zend_Feed_Builder</code> serves as a concrete implementation to demonstrate the usage. Users
                    are encouraged to make their own classes to implement <code class="code">Zend_Feed_Builder_Interface</code>.
                </p></td></tr>
</table></div>
<p>
                Here is an example of <code class="code">Zend_Feed::importBuilder()</code> usage:
            </p>
<pre class="programlisting">&lt;?php
// importing a feed from a custom builder source
$atomFeedFromArray = Zend_Feed::importBuilder(new Zend_Feed_Builder($array));

// the following line is equivalent to the above; by default a Zend_Feed_Atom instance is returned
$atomFeedFromArray = Zend_Feed::importArray(new Zend_Feed_Builder($array), 'atom');

// importing a rss feed from a custom builder array
$rssFeedFromArray = Zend_Feed::importArray(new Zend_Feed_Builder($array), 'rss');
            </pre>
</div>
<div class="sect3" lang="en">
<div class="titlepage"><div><div><h4 class="title">
<a name="zend.feed.importing.custom.dump"></a>15.2.1.3. Dumping the contents of a feed</h4></div></div></div>
<p>
                To dump the contents of a <code class="code">Zend_Feed_Abstract</code> instance, you may use <code class="code">send()</code> or
                <code class="code">saveXml()</code> methods.
            </p>
<pre class="programlisting">&lt;?php
assert($feed instanceof Zend_Feed_Abstract);

// dump the feed to standard output
print $feed-&gt;saveXML();

// send http headers and dump the feed
$feed-&gt;send();
            </pre>
</div>
</div>
</div>
<div class="navfooter"><table width="100%" summary="Navigation footer">
<tr>
<td width="40%" align="left">
<a accesskey="p" href="zend.feed.html">Prev</a> </td>
<td width="20%" align="center"><a accesskey="u" href="zend.feed.html">Up</a></td>
<td width="40%" align="right"> <a accesskey="n" href="zend.feed.findFeeds.html">Next</a>
</td>
</tr>
<tr>
<td width="40%" align="left" valign="top">Chapter 15. Zend_Feed </td>
<td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td>
<td width="40%" align="right" valign="top"> 15.3. Retrieving Feeds from Web Pages</td>
</tr>
</table></div>
<div class="revinfo"></div>
</body>
</html>
